package dao;

import entity.Purchase;
import entity.Sale;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class PurchaseDao extends BaseDao {
    //列表所有进货单信息
    public List getAllpurchase() throws Exception {
        List purchaseList = new ArrayList();
        String sql = "select purchaseId,supplierId,handlerName,purchaseTotalPrice,purchaseDate" +
                " from purchase";
        Connection connection = getConnection();//获取数据库连接
        Statement statement = connection.createStatement();//创建statement对象
        ResultSet resultSet = statement.executeQuery(sql);//执行查询sql
        while(resultSet.next()){
            String purchaseId = resultSet.getString("purchaseId");
            String supplierId = resultSet.getString("supplierId");
            String handlerName = resultSet.getString("handlerName");
            float purchasePrice = resultSet.getFloat("purchaseTotalPrice");
            String  purchaseDate = resultSet.getString("purchaseDate");
            Purchase purchase = new Purchase(purchaseId,supplierId,handlerName,purchasePrice,purchaseDate);
            purchaseList.add(purchase);
        }
        return purchaseList;
    }

    //录入供货单信息
    public boolean addPurchase(Purchase purchase) throws Exception {
        String sql = "insert into purchase(purchaseId,supplierId,handlerName,purchaseTotalPrice,purchaseDate)" +
                " VALUES(?,?,?,?,?)";
        Connection connection = getConnection();//获取数据库连接
        PreparedStatement preparedStatement = connection.prepareStatement(sql);//创建PreparedStatement
        preparedStatement.setString(1, purchase.getPurchaseId());
        //设置第一个？占位符的值
        preparedStatement.setString(2, purchase.getSupplierId());
        //设置第二个？占位符的值
        preparedStatement.setString(3, purchase.getHandlerName());
        //设置第三个？占位符的值
        preparedStatement.setFloat(4, purchase.getPurchaseTotalPrice());
        //设置第四个？占位符的值
        preparedStatement.setString(5, purchase.getPurchaseDate());
        //设置第五个？占位符的值

        int num = preparedStatement.executeUpdate();//执行插入sql语句
        if (num==1)
        {
            return true;//执行成功
        }else {
            return false;//执行失败
        }
    }

    //列表指定编号供货单信息
    public Purchase getAppointPurchase2(String purchaseIdTemp) throws Exception {
        Purchase purchase = new Purchase();
        String sql = "select purchaseId,supplierId,handlerName,purchaseTotalPrice,purchaseDate" +
                " from purchase where purchaseId='" + purchaseIdTemp + "'";
        Connection connection = getConnection();//获取数据库连接
        Statement statement = connection.createStatement();//创建statement对象
        ResultSet resultSet = statement.executeQuery(sql);//执行查询sql
        while(resultSet.next()){
            String purchaseId = resultSet.getString("purchaseId");
            String userId = resultSet.getString("supplierId");
            String handlerName = resultSet.getString("handlerName");
            float purchaseTotalPrice = resultSet.getFloat("purchaseTotalPrice");
            String  purchaseDate = resultSet.getString("purchaseDate");
            purchase = new Purchase(purchaseId,userId,handlerName,purchaseTotalPrice,purchaseDate);
        }
        return purchase;
    }

    //删除供货单信息
    public String deletePurchase(Purchase purchase) throws Exception {
        String result= null;
        Connection connection = getConnection();//获取数据库连接

        String sql = "delete from purchase where purchaseId=?";//定义sql语句
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,purchase.getPurchaseId());
        int num = preparedStatement.executeUpdate();//执行删除sql操作
        if(num==1){
            //删除成功
            result = "删除成功";
        }else {
            //删除失败
            result = "删除失败";
        }
        return result;
    }

    public static void main(String[] args) throws Exception {
        PurchaseDao purchaseDao = new PurchaseDao();
        System.out.println(purchaseDao.getAllpurchase());
    }
}
